<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>promise</title>
</head>
<body>
    

    <script>
        
        {

            function fn () {
                return new Promise((resolve,reject)=>{
                        
                    setTimeout(() => {
                        resolve('abc');
                    }, 1000);

                });
            }

            function shoInfo (data) {
                alert(data);
            }

            let f = fn();

            f.then(shoInfo);

        }

        {
            console.dir(Promise);
        }

        {
            let p1 = Promise.resolve('123');
            let p2 = new Promise((resolve,reject)=>{
                resolve('123')
            });
            console.log(p1);
            console.log(p2);

        }


        {

            function gg () {
                return new Promise((resolve,reject)=>{
                    resolve({
                        code: 0,
                        msg: 'connection success'
                    })
                });
            }

            let g = gg();

            g.then((d)=>{
                console.log(d.msg);
            });


        }


        {
            let p1 = new Promise( (resolve, reject) => {
                setTimeout(() => resolve('hi'), 3000);
            });

            console.log('p1',p1);

            let p2 = Promise.resolve(p1);

            let p3 = new Promise((resolve,reject)=>{
                resolve(p1);
            });

            p1.then((result)=>{
                console.log('result',result);
            });

            console.log('p2',p2);

            console.log('p3',p3);
        }

        {
            let pro1 = new Promise((resolve,reject)=>{
                if (true) {
                    resolve('success');
                } else {
                    reject('error');
                }
            });

            pro1.then((d)=>{
                console.log('d',d);
            });
        }


        {
            let pp1 = Promise.resolve('123');

            pp1.then((d)=>{
                console.log('pp1',d);
            });

            let pp2 = new Promise((resolve,reject)=>{
                resolve('123');
            });

            pp2.then((da)=>{
                console.log('pp2',da);
            });


        }

        {

            function timeDelay (timeout) {
                return new Promise((resolve,reject)=>{
                    setTimeout(() => {
                        console.log('this is timeout');
                    }, timeout);
                });
            }

            Promise.race([
                new Promise((resolve,reject)=>{
                    resolve('promise object');
                }).then((d)=>{
                    console.log(d);
                }),
                timeDelay (3000)
            ]).catch((err)=>{
                    console.log('err',err);
                });

        }

        {

            function showMsg (d) {
                return new Promise((resolve,reject)=>{
                    resolve(d);
                });
            }

            let p1 = showMsg('我是');

            let p2 = showMsg('coder');

            let p3 = showMsg(',你呢');

            Promise.all([
                p1,p2,p3
            ]).then((da)=>{
                console.log('all',da);
            })

        }

        {
            // then 的返回值

            let p1 = new Promise((resolve,reject)=>{
                resolve(1);
            });

            let result = p1.then((d)=>{
                return d*2;
            });

            console.log('then 的返回值',result);



        }
    </script>
</body>
</html>